package com.example.study_manger.control;

import com.example.study_manger.entity.ApplyPermission;
import com.example.study_manger.entity.UserTb;
import com.example.study_manger.pojo.PageResult;
import com.example.study_manger.repository.ApplyPermissionRe;
import com.example.study_manger.repository.UserTbRe;
import com.example.study_manger.tools.R;
import jakarta.persistence.criteria.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/permissions")
public class PermissonControl {
    @Autowired
    private ApplyPermissionRe permissionRe;
    @Autowired
    private UserTbRe userTbRe;
    @GetMapping
    public R<PageResult> getPermissions(@RequestParam(name = "username",required = false) String username,
                                        @RequestParam(name = "organizationId",required = false) Long organizationId,
                                        @RequestParam(name = "page") int page,
                                        @RequestParam(name = "size") int size){
        System.out.println(username);
        System.out.println(organizationId);
        Pageable pageable = PageRequest.of(page - 1, size);  // PageRequest的页码从0开始

        // 构建动态查询条件
        Specification<ApplyPermission> spec = (root, query, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            // 根据username添加查询条件
            if (username != null && !username.isEmpty()) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.like(root.get("username"), "%" + username + "%"));
            }
            // 根据organizationId添加查询条件
            if (organizationId != null) {
                // 通过role->organizationId进行关联查询
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("role").get("organization").get("id"), organizationId));
            }
            return predicate;  // 返回动态构建的条件
        };

        // 使用Specification查询数据并分页
        Page<ApplyPermission> applyPermissions = permissionRe.findAll(spec, pageable);
        return R.success(new PageResult(applyPermissions.getContent(),applyPermissions.getTotalElements()));
    }
    @GetMapping("/{id}")
    public R<UserTb> getPermission(@PathVariable Long id){
        ApplyPermission  applyPermission = permissionRe.findById(id).get();
        UserTb userTb = new UserTb(null, applyPermission.getUsername(), applyPermission.getPassword(),
                applyPermission.getEmail(), applyPermission.getUrl(), applyPermission.getRole());
        UserTb save = userTbRe.save(userTb);
        if(save!=null){
            permissionRe.deleteById(id);
            return R.success(save);
        }
        return R.fail("授权失败");

    }
    @DeleteMapping("/{id}")
    public R<String> deletePermission(@PathVariable Long id){
        try {
            permissionRe.deleteById(id);
            return R.success("删除成功");
        }catch (Exception e){
            return R.fail("删除失败");
        }
    }
}
